<!--#include file="./doctype.html"-->
<title>上传文件管理-<%=siteTitle%>-后台管理</title>
</head><body>
<div class="wrap">
    <ol class="breadcrumb">
        <li class="active"><a href="<%=fileName%>">列表首页</a></li>
    </ol>
    <form class="form-inline mb10 mt10" role="form">
        <input type="hidden" id="path" name="path" value="../<%=uploadDir%>/">
        <div class="form-group">
            排序方式:
            <label class="btn btn-sm btn-default inline mr10 order">
                <input type="radio" name="order" value="name" checked="checked"> 名称
            </label>
            <label class="btn btn-sm btn-default inline mr10 order">
                <input type="radio" name="order" value="size"> 大小
            </label>
            <label class="btn btn-sm btn-default inline mr10 order">
                <input type="radio" name="order" value="type"> 类型
            </label>
        </div>
        <div class="form-group">
            <span class="btn btn-sm btn-default mr15" id="move_up_dir">
                <i class="glyphicon glyphicon-arrow-up"></i> 上一级目录
            </span>
            <span class="text-primary" id="current_dir">../<%=uploadDir%>/</span>
        </div>
    </form>
    <div id="file_box" class="file_box"></div>
    <div class="btn-group clearfix">
        <span class="btn btn-sm btn-default selectFile">
            <i class="glyphicon glyphicon-th"></i> 全选/反选
        </span>
        <span class="btn btn-sm btn-default imageWatermark">
            <i class="glyphicon glyphicon-certificate"></i> 打水印
        </span>
        <span class="btn btn-sm btn-default deleteBatch">
            <i class="glyphicon glyphicon-trash"></i> 删除
        </span>
    </div>
    <!--#include file="loadTime.html"-->
</div>
<script>
$(document).ready(function(){
    var cache = {}, page = $.cookie('page') || '<%=page%>', rows = $.cookie('rows') || '<%=rows%>',
    file_box = $('#file_box'), path = $('#path'), defPath = "../<%=uploadDir%>/",
    current_dir = $('#current_dir'),
    no_data = '<h3 class="text-primary"><i class="glyphicon glyphicon-info-sign mr10"></i> 没有文件</h3>',
    show = function() {
        if($.isEmptyObject(cache)) {
            return file_box.html(no_data);
        }
        var a = cache, v = [], list = a.file_list, total = a.total_count,
        total_page = Math.ceil(total / rows);
        if (page > total_page) {
            page = total_page;
        }
        var position = cj.int(page * rows - 1, 0),
        begin = page > 1 ? cj.int((page - 1) * rows, 0) : page - 1;
        if(position >= list.length) {
            position = list.length - 1;
        }
        v.push('<div class="text-center clearfix">');
        v.push($.page({
            total: total, page: page, rows: rows, down: 'down'
        }));
        v.push('</div>');
        v.push('<div class="clearfix">');
        for(; begin <= position; begin++) {
            var n = list[begin];
            v.push('<div class="col-sm-8 col-md-2 file_item">');
            v.push('<div class="thumbnail">');
            if(n.is_dir) {
                v.push('<div class="folder text-center" data-path="'+a.current_dir_path+n.filename+'/">');
                v.push('<i class="glyphicon glyphicon-folder-open file_icon"></i>');
                v.push('</div>');
            } else {
                if(n.is_photo) {
                    v.push('<img src="'+a.current_url+n.filename+'"');
                    v.push(' title="'+n.filename+'" class="show_pic">');
                } else {
                    v.push('<div class="text-center">');
                    v.push('<i class="glyphicon glyphicon-file file_icon"></i>');
                    v.push('</div>');
                }
            }
            var size = cj.toSize(n.filesize);
            v.push('<div class="caption" title="');
            v.push('name: ' + n.filename + '\n');
            v.push('size: ' + size + '\n');
            v.push('type: ' + n.filetype + '\n');
            v.push('date: ' + n.datetime);
            v.push('">');
            v.push('<label class="btn-block"><input type="checkbox" name="fn" value="');
            v.push(a.current_url+n.filename+'" /> ' + n.filename + '</label>');

            v.push('<p>' +(n.is_dir ? '' : size+', ')+n.filetype+ '</p>');
            v.push('<p>'+n.datetime+'</p>');
            v.push('</div>');

            v.push('</div>');
            v.push('</div>');
        }
        v.push('</div>');
        v.push('<div class="text-center clearfix">');
        v.push($.page({
            total: total, page: page, rows: rows, down: 'up'
        }));
        v.push('</div>');
        file_box.html(v.join(''));
    },
    out = function(a) {
        if($.isEmptyObject(a)) {
            return file_box.html(no_data);
        }
        if(a.error != '0') {
            return file_box.html(a.message);
        }
        current_dir.html('当前目录: '+a.current_dir_path+', 文件总数:'+a.total_count);
        if(a.total_count == 0) {
            return file_box.html(no_data);
        }
        cache = a;
        show();
    },
    getData = function() {
        var p = path.val();
        p != c_path && $.cookie('fileDir', p);
        $.json({
            url: './file_manage.asp',
            data: $('form').serializeArray()
        }, out, function() {
            $.cookie('fileDir', defPath); //防止目录被其他人删除，重新记录目录
            location.reload(true);
        });
    },
    c_path = $.cookie('fileDir');
    if(c_path != null) {
        path.val(c_path);
    }
    getData();
$(this)
	.on('click', '.folder', function() {
		path.val($(this).attr('data-path'));
		getData();
	})
	.on('click', '#move_up_dir', function() {
		var p = path.val();
		if(p == '' || p == defPath) {
		    return false;
		}
		p = p.substring(0, p.length-1);
        p = p.substring(0, p.lastIndexOf('/')+1);
		path.val(p);
		$.cookie('fileDir', p);
		getData();
	})
	.on('click', 'input[name="order"]', function() {
		getData();
	})
	//全选、反选
	.on('click', '.selectFile', function() {
		$('input[name="fn"]').selectAll();
	})
	.on('click', '.deleteBatch', function() {
        $.deleteBatch({
            data: $('input[name="fn"]').serializeArray()
        });
	})
	.on('click', '.imageWatermark', function() {
		var id = $(this).attr('data-id'), url = '<%=fileName%>?q=update&id=' + id;
		$.json(url, function(a) {
			$.message({c: a.message});
		});
	})
	//绑定分页事件
	.on('click', '.pagination a', function(e) {
	    e.preventDefault(); //取消原来的a链接事件
		var h = $(this).attr('href'), p = (/page=(\d+)/ig).exec(h), r = (/rows=(\d+)/ig).exec(h);
		page = p[1] || page;
		rows = r[1] || rows;
		$.cookie('page', page);
		$.cookie('rows', rows);
		show();
	})
});
</script>
</body>
</html>